package edu.vt.ece.rt.benchmark.intruder;

import java.util.Vector;

import edu.vt.ece.rt.quorum.ClustersManager;


public class Intruder {

	static int PARAM_DEFAULT_ATTACK = 10;
	static long PARAM_DEFAULT_LENGTH = 16;
	static long PARAM_DEFAULT_NUM    = 100;
	static int PARAM_DEFAULT_SEED   = 1;
	static int PARAM_DEFAULT_THREAD = 1;
	static int count = 0;
	static int generate = 0;
	
	/* =============================================================================
	 * processPackets
	 * =============================================================================
	 */
	static void
	processPackets (String streamId, Decoder decoderPtr, Vector<Long> errorVectorPtr)
	{

	    Detector detectorPtr = new Detector();
//	    assert(detectorPtr);
//	    detectorPtr, &preprocessor_toLower);

	    
	    while (true) {

	        Packet bytes;
	        // start
	        System.out.print(".");
	        bytes = Stream.stream_getPacket(streamId, null);
//	        bytes = Stream.stream_getPacket(Benchmark.streamPtr);
//	        System.out.println("new packet obtained1!!!!");
	        // end
	        
	        if (bytes == null) {
	        	System.out.println("bytes is null for "+ClustersManager.baseClusterId);
	            break;
	            
	        }

	        Packet packetPtr = bytes;
	        long flowId = packetPtr.flowId;

	        Error.error error;
	        error = Decoder.decoder_process(
	                                  bytes,
	                                  (Packet.PACKET_HEADER_LENGTH + packetPtr.length));
	        
	        if (error != Error.error.ERROR_NONE) {
	            /*
	             * Currently, stream_generate() does not create these errors.
	             */
	            boolean status = errorVectorPtr.add(flowId);
	            assert(status);
	        }

	        String data;
	        Integer decodedFlowId = 1;
	        Pair p = Decoder.decoder_getComplete(decodedFlowId);
	        data = p.str;
	        decodedFlowId = p.i;
	        if (data != null) {
	        	Intruder.count++;

	            Error.error err = Detector.detector_process(detectorPtr, data);
	            if (err != Error.error.ERROR_NONE) {
		            /*
		             * Currently, stream_generate() does not create these errors.
		             */
		            boolean status = errorVectorPtr.add(flowId);
		            assert(status);
		        }
	        }

	    }

	}


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		// variables read by all : streamPtr, fragmentedMap,decodedQueuePtr
		Dictionary dictionaryPtr = new Dictionary();
		Stream streamPtr = new Stream("stream",PARAM_DEFAULT_ATTACK);
        long attack = Stream.stream_generate(streamPtr, dictionaryPtr, PARAM_DEFAULT_NUM, PARAM_DEFAULT_SEED, PARAM_DEFAULT_LENGTH);
        Decoder decoderPtr = new Decoder();
        Vector<Long> errorVectorPtr = new Vector<Long>();
        processPackets(streamPtr.getId(), decoderPtr, errorVectorPtr);

        System.out.println("Generate is "+PARAM_DEFAULT_NUM);        
        System.out.println("Count is "+Intruder.count);
        System.out.println("Attack is "+attack);
        
	}

}
